//
//  AppMixerThread.h
//  Class for managing the thread where the mixers live
//  2006 John Wiggins
//
// This program is free software; you can redistribute it and/or modify  
// it under the terms of the GNU General Public License as published by  
// the Free Software Foundation; either version 2 of the License, or     
// (at your option) any later version.                                   
// 
// This program is distributed in the hope that it will be useful,       
// but WITHOUT ANY WARRANTY; without even the implied warranty of        
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
// GNU General Public License for more details.                          
//     
//     You should have received a copy of the GNU General Public License     
//     along with this program; if not, write to the                         
//         Free Software Foundation, Inc.,                                       
//         59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  
//
//-------------------------------------------------------------
#ifndef APP_MIXER_THREAD_H
#define APP_MIXER_THREAD_H
//-------------------------------------------------------------
#include <map>
#include <utility>
#include <vector>

#include "Ipc/IpcMessage.h"
#include "Ipc/IpcPort.h"
#include "App/AppMixer.h"
#include "App/AppPlugin.h"
#include "App/AppSourceThread.h"
//-------------------------------------------------------------
class AppMixerThread
{
public:
                    AppMixerThread      ();
                    ~AppMixerThread     ();
    
    ipc_port_t      portId              () const;
    status_t        postMessage         ( IpcMessage *msg ) const;
    
private:
    void            receiveMessages     ( bool const waitForMessages );
    void            handleMessage       ( IpcMessage *msg );

    static void *   start_loop          ( void *data );
    void *          main_loop           ();

    void            addFilter           ( ge_mixer_plugin *mixer,
                                          ge_filter_plugin *filter,
                                          ge_filter_plugin *before );
    void            removeFilter        ( ge_mixer_plugin *mixer, ge_filter_plugin *filter );
    void            addMixer            ( ge_mixer_plugin *mixer );
    void            removeMixer         ( ge_mixer_plugin *mixer );
    void            connectOutput       ( ge_output_plugin *output );
    void            disconnectOutput    ( ge_output_plugin *output );
    void            connectSource       ( ge_mixer_plugin *mixer, AppSourceThread *source );
    void            disconnectSource    ( ge_mixer_plugin *mixer, AppSourceThread *source );
    void            updateMixerOrder    ( IpcMessage *msg );
    void            mix                 ();

    
    std::map<ge_mixer_plugin*, AppMixer*>                   mMixerMap;
    std::map<AppMixer*,std::vector<AppSourceThread*> >      mSourceMap;
    std::vector<std::pair<AppMixer*,std::vector<int32> > >  mActiveMixList;
    pthread_t           mThread;
    ipc_port_t          mMsgPort;
    ge_output_plugin *  mOutPlugin;
    bool                mQuitting;
};
//-------------------------------------------------------------
ipc_port_t
AppMixerThread::portId() const
{
    return mMsgPort;
}
//-------------------------------------------------------------
status_t
AppMixerThread::postMessage( IpcMessage *msg ) const
{
    return msg->SendMessage(mMsgPort);
}
//-------------------------------------------------------------
#endif // APP_MIXER_THREAD_H
